package cn.itcast.zuoyebang;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.itcast.zuoyebang.fragment.FragmentAdapter;
import cn.itcast.zuoyebang.fragment.table2fragment.Table2Fragment;
import cn.itcast.zuoyebang.fragment.Table3Fragment;
import cn.itcast.zuoyebang.fragment.Table4Fragment;
import cn.itcast.zuoyebang.fragment.tablefragment.TableFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager viewPager;
    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.view_pager);

        List<Fragment> list= new ArrayList<>();
        TableFragment tableFragment = new TableFragment();
        Table2Fragment table2Fragment = new Table2Fragment();
        Table3Fragment table3Fragment = new Table3Fragment();
        Table4Fragment table4Fragment = new Table4Fragment();

        list.add(tableFragment);
        list.add(table2Fragment);
        list.add(table3Fragment);
        list.add(table4Fragment);

        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(adapter);

        LinearLayout tab1 = findViewById(R.id.tab1);
        tab1.setOnClickListener(this);
        LinearLayout tab2 = findViewById(R.id.tab2);
        tab2.setOnClickListener(this);
        LinearLayout tab3 = findViewById(R.id.tab3);
        tab3.setOnClickListener(this);
        LinearLayout tab4 = findViewById(R.id.tab4);
        tab4.setOnClickListener(this);

        img1 = findViewById(R.id.iv1);
        img2 = findViewById(R.id.iv2);
        img3 = findViewById(R.id.iv3);
        img4 = findViewById(R.id.iv4);

        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);

        img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
        tv1.setTextColor(getResources().getColor(R.color.yellow));

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setNormal();
                if(position == 0) {
                    img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
                    tv1.setTextColor(getResources().getColor(R.color.yellow));
                } else if(position == 1) {
                    img2.setBackgroundResource(R.drawable.home_page_tab2_selected);
                    tv2.setTextColor(getResources().getColor(R.color.yellow));
                } else if(position == 2) {
                    img3.setBackgroundResource(R.drawable.home_page_tab3_selected);
                    tv3.setTextColor(getResources().getColor(R.color.yellow));
                } else if(position == 3) {
                    img4.setBackgroundResource(R.drawable.home_page_tab4_selected);
                    tv4.setTextColor(getResources().getColor(R.color.yellow));
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tab1:
                viewPager.setCurrentItem(0);
                img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
                tv1.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab2:
                viewPager.setCurrentItem(1);
                img2.setBackgroundResource(R.drawable.home_page_tab2_selected);
                tv2.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab3:
                viewPager.setCurrentItem(2);
                img3.setBackgroundResource(R.drawable.home_page_tab3_selected);
                tv3.setTextColor(getResources().getColor(R.color.yellow));
                break;
            case R.id.tab4:
                viewPager.setCurrentItem(3);
                img4.setBackgroundResource(R.drawable.home_page_tab4_selected);
                tv4.setTextColor(getResources().getColor(R.color.yellow));
                break;
        }

    }

    private void setNormal() {
        img1.setBackgroundResource(R.drawable.home_page_tab1_normal);
        img2.setBackgroundResource(R.drawable.home_page_tab2_normal);
        img3.setBackgroundResource(R.drawable.home_page_tab3_normal);
        img4.setBackgroundResource(R.drawable.home_page_tab4_normal);
        tv1.setTextColor(getResources().getColor(R.color.gray));
        tv2.setTextColor(getResources().getColor(R.color.gray));
        tv3.setTextColor(getResources().getColor(R.color.gray));
        tv4.setTextColor(getResources().getColor(R.color.gray));
    }
}